#!/bin/bash
if [ ! -f /usr/local/backups/config ]; then
	echo "Can't find config"
	logger -t "DuplicityBackup:$0" "Can't find config"
	exit
fi

if [ -z $1 ]; then
	echo "Param 1 must be a directory to backup"
	logger -t "DuplicityBackup:$0" "Param 1 must be a directory to backup"
	exit
fi

if [ ! -d $1 ]; then
	logger -t "DuplicityBackup:$0" "The dir '$1' does not exist"
	echo "The dir '$1' does not exist"
	echo "Param 1 must be a directory to backup"
	exit
fi
SOURCE_DIR=$1

if [ -z $2 ]; then
	logger -t "DuplicityBackup:$0" "Param 2 must be an S3 subdirectory to target"
	echo "Param 2 must be an S3 subdirectory to target"
	exit
fi
S3DIR=$2

logger -t "DuplicityBackup:$SOURCE_DIR" "Start of backup"
(
source /usr/local/backups/config

DEST=$DEST_ROOT/$2

$DUPLICITY $DUPLICITY_OPTION $SOURCE_DIR $DEST

$DUPLICITY remove-all-but-n-full $FULLS_TO_KEEP --force \
  --encrypt-key=$GPG_ENCR_KEY \
  --sign-key=$GPG_SIGN_KEY $DEST


export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export PASSPHRASE=
export SIGN_PASSPHRASE=


) | logger -t "DuplicityBackup:$SOURCE_DIR"
logger -t "DuplicityBackup:$SOURCE_DIR" "End of backup"
